<template>
  <div class="box">
    <el-form
      ref="form"
      :rules="rules"
      :model="form"
      label-width="100px"
      class="demo-dynamic"
    >
      <div class="img">
        <el-image
          style="width: 130px; height: 130px"
          src="http://weldon.net.cn/back/img/logo2.fc048282.png"
        ></el-image>
      </div>
      <div class="formbox">
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            prefix-icon="el-icon-user"
            style="width: 100%"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            prefix-icon="el-icon-goods"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button>重置</el-button>
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "login-vue",
  data() {
    return {
      form: {
        username: "admin",
        password: "123456",
      },
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: ["blur", "change"],
          },
          {
            min: 5,
            max: 11,
            message: "长度在 5 到 11 个字符",
            trigger: ["blur", "change"],
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: ["blur", "change"],
          },
          {
            pattern: /^\w{5,11}$/,
            message: "请输入 5 到 10 位的密码",
            trigger: ["blur", "change"],
          },
        ],
      },
    };
  },
  methods: {
    async submit() {
      await this.$refs.form.validate();
      await this.$store.dispatch("user/login", this.form);
      this.$router.push("/");
    },
  },
};
</script>

<style lang="less" scoped>
.box {
  background-color: #2b4b6b;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  .demo-dynamic {
    position: relative;
    width: 450px;
    height: 300px;
    background-color: #fff;
    border: 1px solid #ccc;
    display: flex;
    justify-content: center;
    align-items: center;
    .formbox {
      margin-top: 40px;
      width: 400px;
      /deep/ .el-form-item__content {
        margin-left: 0px !important;
      }
    }
    .img {
      position: absolute;
      top: 0;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: #ccc;
      border-radius: 50%;
    }
  }
}
</style>
